# 30. 数组去重和排序
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.on('line', function(line) {
const inputArray = line.split(',');
const countMap = {};
const firstMap = {};
for(let i=0; i<inputArray.length; i++) {
let v = inputArray[i];
countMap[v] = countMap[v] ? (countMap[v] + 1) : 1;
if (!firstMap[v]) {
firstMap[v] = i;
}
}
const sortArr = firstMap.keys();
sortArr.sort((a, b) => {
const countA = countMap[a];
const countB = countMap[b];
if (countA !== countB) {
return countB - countA;
} else {
const firstA = firstMap[a];
const firstB = firstMap[b];
return firstA - firstB;
}
})
let res = '';
for(let i=0; i<sortArr.length; i++) {
res += sortArr[i];
if (i !== sortArr.length - 1) {
res += ',';
}
}
console.log(res);
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39